﻿Imports RunEnv
Public Class CUser
    Private Const SPLITTAG As String = "."

    Private mcollRight As Collection
    Private mblnIsAdmin As Boolean
    Public Property UserID As String
    Public Property UserName As String

    Public ReadOnly Property IsAdmin As Boolean
        Get
            Return mblnIsAdmin
        End Get
    End Property

    Public ReadOnly Property CollRight As Collection
        Get
            If mcollRight Is Nothing Then
                InitCollRight()
            End If
            Return mcollRight
        End Get
    End Property

    '初始化权限集合
    Private Sub InitCollRight()
        Dim sql As String
        Dim dt As DataTable
        Dim key As String
        sql = "Select B_URSysFunc.SubSystemID,B_URSubSystem.SubSystem,B_URSysfunc.FuncName," _
            & " B_UserRight.RightValue" _
            & " From B_UserRight" _
            & " Left Join B_URSysFunc On B_UserRight.RightID = B_URSysFunc.RightID" _
            & " Left Join B_URSubSystem On B_URSysFunc.SubSystemID = B_URSubSystem.SubSystemID" _
            & " Where B_UserRight.UserID='" & UserID & "'"
        dt = CRunEnv.DBConnection.GetDataTable(sql)
        mcollRight = New Collection
        For Each dtRow As DataRow In dt.Rows
            key = dtRow.Item("SubSystem") & SPLITTAG & dtRow.Item("FuncName")
            mcollRight.Add(dtRow.Item("RightValue"), key)
        Next
    End Sub

    Public Function GetRight(ByVal subsystem As String, ByVal funcName As String) As CRight
        Dim objRight As New CRight
        Dim key As String
        Try
            If Me.IsAdmin Then
                objRight.RightValue = 63
                Return objRight
            End If

            key = subsystem & SPLITTAG & funcName
            If CollRight.Contains(key) Then
                objRight.RightValue = CollRight.Item(key)
            Else
                objRight.RightValue = 0
            End If
            Return objRight
        Catch ex As Exception
            MsgBox("权限获取失败，" & ex.Message, MsgBoxStyle.Exclamation, "系统提示")
            objRight.RightValue = 0
            Return objRight
        End Try
    End Function
End Class
